Skip to content

fix(paywall): use dynamic token decimals instead of hardcoding 6#1980

Open
ryanRfox wants to merge 3 commits intox402-foundation:mainfrom
ryanRfox:fix/paywall-dynamic-decimals
Open

fix(paywall): use dynamic token decimals instead of hardcoding 6#1980
ryanRfox wants to merge 3 commits intox402-foundation:mainfrom
ryanRfox:fix/paywall-dynamic-decimals

Conversation

@ryanRfox
Copy link
Copy Markdown
Contributor

@ryanRfox ryanRfox commented Apr 9, 2026

Description

The EVM paywall hardcodes / 1000000 everywhere, breaking display amounts for any token that isn't a 6-decimal stablecoin. An 18-decimal token priced at 0.001 raw units renders as $1,000,000,000. The assumption appears in three places — server-side HTML generation, client-side balance display, and a USDC-only address lookup table.

Closes #1979.

Approach

The PR replaces the hardcoded-6-decimal assumption on both sides of the paywall:

Server side (@x402/paywall/src/evm/index.ts)

  • Replace the hardcoded / 1000000 divisor with a lookup against @x402/evm's DEFAULT_STABLECOINS registry, with a fallback to 6 (USDC default) when the network isn't in the registry.
  • Replace the lossy parseFloat(amount) / 10**decimals math with Number(formatUnits(BigInt(amount), decimals)) so atomic-to-display conversion preserves precision through 18-decimal amounts. BigInt also rejects non-integer atomic strings, which matches the spec's atomic-integer contract.

Client side (@x402/paywall/src/evm/EvmPaywall.tsx)

  • Read the token contract address from the payment requirement's asset field (server populates it).
  • Query the standard ERC-20 decimals() function on-chain via the new getTokenDecimals helper in utils.ts, so balance formatting uses the actual token's precision.
  • State variable renamed formattedUsdcBalanceformattedBalance; balance-check callback renamed checkUSDCBalancecheckBalance.

Internal utility (@x402/paywall/src/evm/utils.ts)

  • Replace the internal getUSDCBalance(client, address) — which hardcoded USDC contract addresses for three chains (Ethereum, Base, Base Sepolia) and returned 0n for every other chain — with generic:
    • getTokenBalance(client, owner, tokenAddress) — ERC-20 balanceOf against the supplied token address
    • getTokenDecimals(client, tokenAddress) — ERC-20 decimals() against the supplied token address, with a 6 fallback on error
  • Drops the USDC_ADDRESSES map entirely.
  • Breaking change (internal): getUSDCBalance is removed. This helper was not part of the package's public API (exports map); it was only consumed by the bundled EvmPaywall React component inside the paywall HTML template. No server-operator code path calls it. Anyone who has forked the paywall and imports from the internal file path (bypassing package exports) would need to update their import and call site.

@x402/evm public API

@x402/evm now publicly re-exports DEFAULT_STABLECOINS from ./shared/defaultAssets so consumers can read the canonical default-asset registry directly.

Generated templates

The baked EVM paywall bundles (typescript/packages/http/paywall/src/evm/gen/template.ts, go/http/evm_paywall_template.go, python/x402/http/paywall/evm_paywall_template.py) are regenerated to carry the updated EvmPaywall.tsx / utils.ts. SVM templates are regenerated for toolchain parity — the build step emits both.

Why this approach

@x402/evm's DEFAULT_STABLECOINS registry already holds the decimals field for every supported chain (eip155:31611 / Mezo mUSD = 18, eip155:4326 / MegaETH MegaUSD = 18, every other chain's default = 6). The same registry powers existing dispatch paths in the SDK:

  • scheme.getAssetDecimals(asset, network) on both the exact and upto EVM schemes
  • The inline scheme?.getAssetDecimals?.(...) dispatch in @x402/core's x402ResourceServer (server.ts:795)

The paywall now consolidates the registry lookup so all consumers share the same source within a published version, rather than carrying a parallel map. A new chain added to DEFAULT_STABLECOINS is picked up by the paywall when the consumer upgrades @x402/evm — no paywall rebuild required.

@x402/paywall already imports from @x402/evm for client-side code; this PR extends that to the server-side decimals lookup.

Tests

pnpm --filter @x402/paywall test --run network-handlers — 12/12 pass (6 new tests added):

  • getDefaultTokenDecimals returns 18 for Mezo Testnet (eip155:31611)
  • getDefaultTokenDecimals returns 6 for Base mainnet (eip155:8453), with DEFAULT_STABLECOINS["eip155:8453"] asserted alongside so an empty registry would fail the test
  • getDefaultTokenDecimals falls back to 6 for networks not in the registry
  • evmPaywall.generateHtml end-to-end on a Mezo Testnet 18-decimal payment: 1000000000000000 atomic renders as amount: 0.001, in the inline window.x402 script (the regression test for the / 1e6 order-of-magnitude bug)
  • evmPaywall.generateHtml end-to-end on a Base mainnet 6-decimal payment: 1000000 atomic renders as amount: 1,
  • evmPaywall.generateHtml throws on a non-integer atomic amount string ("1.5"), pinning the BigInt strictness so a future revert to parseFloat would fail

@github-actions github-actions bot added typescript go sdk Changes to core v2 packages python labels Apr 9, 2026
@ryanRfox ryanRfox force-pushed the fix/paywall-dynamic-decimals branch 2 times, most recently from f08dc05 to 9a67807 Compare April 10, 2026 01:39
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 10, 2026

@ryanRfox is attempting to deploy a commit to the Coinbase Team on Vercel.

A member of the Team first needs to authorize it.

@ryanRfox
Copy link
Copy Markdown
Contributor Author

Note: #1926 fixes the same hardcoded-decimals bug in the server-side fallback paywall (x402HTTPResourceServer). This PR fixes the client-side React paywall (@x402/paywall). The two PRs are complementary — different packages, different code paths, both need the fix.

@phdargen phdargen self-assigned this Apr 11, 2026
@ryanRfox ryanRfox force-pushed the fix/paywall-dynamic-decimals branch 2 times, most recently from 6dad9dd to 178b613 Compare April 11, 2026 12:49
@ryanRfox
Copy link
Copy Markdown
Contributor Author

@phdargen this one is ready for review. Hope you can consider this in parallel with #1926 to ensure @natsukingly and I land a complete fix.

I saw your comment recently about the next SDK release coming early this week, so hoping these can make it in as well.

The EVM paywall displays incorrect amounts for any token that does not
use 6 decimal places. An 18-decimal token priced at 0.001 raw units
renders as $1,000,000,000 because the code divides by 10^6 everywhere.

Root cause: the paywall was built assuming USDC (6 decimals) is the
only payment token. This assumption appears in three places:

1. Server-side HTML generation (index.ts) divides the raw amount by
   a hardcoded 10^6 to produce the display value injected into the
   template.

2. Client-side balance display (EvmPaywall.tsx) calls formatUnits
   with a hardcoded 6 and reads the token address from a 3-chain
   USDC lookup table that returns 0 for any other chain.

3. The balance utility (utils.ts) only knows USDC addresses on
   Ethereum, Base, and Base Sepolia — every other chain silently
   shows a zero balance.

Fix:

Server-side (index.ts): look up the token's decimal precision from a
known-decimals map aligned with DEFAULT_STABLECOINS. Only non-6-decimal
chains need entries; everything else falls back to 6. This keeps the
server-side path simple with no RPC calls.

Client-side (EvmPaywall.tsx): read the token contract address from
the payment requirement's `asset` field (which the server already
populates) and query the standard ERC-20 decimals() function on-chain.
This works for any compliant token without maintaining a lookup table.

utils.ts: replace the USDC-specific getUSDCBalance (internal helper,
not part of the public API) with generic getTokenBalance and
getTokenDecimals functions that accept a token address parameter.
The function signature changes from 2 args to 3, but no external
consumer can import it — it is only used by the bundled React
component inside the paywall HTML template.

**Breaking change note:** the internal helper getUSDCBalance in utils.ts
is removed and replaced by getTokenBalance (different signature: takes
an explicit token address instead of looking it up by chain ID). This
function is not exported from the package's public API — it is only
consumed by the bundled EvmPaywall React component inside the paywall
HTML template. No server operator code calls it directly. However,
anyone who has forked the paywall and imports from the file path
(bypassing package exports) would need to update their import and
call site.

Regenerated Go, Python, and TS paywall templates.

**AI disclosure:** This PR was prepared with the assistance of a coding agent and
reviewed by Ryan R. Fox (an actual human) before submission.
@ryanRfox ryanRfox force-pushed the fix/paywall-dynamic-decimals branch from 178b613 to 55da33a Compare April 14, 2026 15:28
Removes the parallel KNOWN_DECIMALS map in @x402/paywall/src/evm/index.ts
that mirrored DEFAULT_STABLECOINS from @x402/evm. The paywall now resolves
token decimals through the same registry that scheme implementations and
@x402/core's inline scheme dispatch already read from.

evmPaywall.generateHtml changes:
  * getDefaultTokenDecimals helper looks up requirement.network directly in
    @x402/evm's DEFAULT_STABLECOINS, with a 6 (USDC default) fallback when
    the network is unknown.
  * Atomic-to-display conversion uses Number(formatUnits(BigInt(amount),
    decimals)) instead of parseFloat(amount) / 10**decimals. parseFloat
    silently rounds sub-cent precision on real 18-decimal amounts before
    the divide; BigInt + formatUnits preserves precision through the
    conversion. BigInt also rejects non-integer atomic strings, which
    matches the spec's atomic-integer contract.

@x402/paywall package.json: @x402/evm moves from devDependencies to
dependencies because the new server-side import of DEFAULT_STABLECOINS is
on the runtime path emitted into dist/.

@x402/evm now publicly re-exports DEFAULT_STABLECOINS from
./shared/defaultAssets so consumers can read the canonical default-asset
registry directly. Both @x402/evm and @x402/paywall get a minor bump
for the additive public API.

Adds 6 unit tests:
  * getDefaultTokenDecimals returns 18 for Mezo Testnet (registry path)
  * getDefaultTokenDecimals returns 6 for Base mainnet, asserted alongside
    DEFAULT_STABLECOINS so an empty registry would fail the test
  * getDefaultTokenDecimals falls back to 6 for unknown networks
  * evmPaywall.generateHtml renders 1e15-atomic Mezo mUSD as
    'amount: 0.001,' end-to-end (regression test for the /1e6 bug)
  * evmPaywall.generateHtml renders 1e6-atomic Base USDC as 'amount: 1,'
  * evmPaywall.generateHtml throws on a non-integer atomic ('1.5'),
    pinning the BigInt strictness so a future revert to parseFloat fails

Closes x402-foundation#1979.
@ryanRfox ryanRfox force-pushed the fix/paywall-dynamic-decimals branch from 3b2fc7f to 117b8fc Compare April 16, 2026 02:56
@github-actions github-actions bot added the examples Changes to examples label Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples Changes to examples go python sdk Changes to core v2 packages typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(paywall): EVM paywall hardcodes 6 decimals — breaks non-6-decimal tokens

2 participants